package maldade.action.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.log4j.Logger;

/**
 * This tool facilitates the data recovery from the request.
 * Singleton class.
 * 
 * @author rnojiri
 */
public class RequestTool
{
	private static Logger log = Logger.getLogger(RequestTool.class);
	
	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
	
	/**
	 * This cannot be instantiated.
	 */
	private RequestTool()
	{
		
	}
	
	/**
	 * Returns a String from request.
	 * 
	 * @param request
	 * @return String
	 */
	public static String getString(String parameterName, boolean putBack, HttpServletRequest request)
	{
		String parameter = request.getParameter(parameterName);
		
		if(isBlank(parameter)) return null;
		
		parameter = StringEscapeUtils.escapeHtml(parameter);
		
		if(putBack) request.setAttribute(parameterName, parameter);
		
		return parameter;
		
	}
	
	/**
	 * Returns an Integer from request.
	 * 
	 * @param request
	 * @return Integer
	 */
	public static Integer getInteger(String parameterName, boolean putBack, HttpServletRequest request)
	{
		String parameter = request.getParameter(parameterName);
		
		if(isBlank(parameter)) return null;
		
		try
		{
			Integer num = Integer.parseInt(parameter);
			
			if(putBack) request.setAttribute(parameterName, num);
			
			return num;
		}
		catch(Exception e)
		{
			log.error("Error parsing parameter " + parameterName + ".");
			
			return null;
		}
	}
	
	/**
	 * Returns a check box value from request.
	 * 
	 * @param checkBoxName
	 * @param putBack
	 * @param request
	 * @return boolean
	 */
	public static boolean getCheckBoxValue(String checkBoxName, boolean putBack, HttpServletRequest request)
	{
		String checkBox = request.getParameter(checkBoxName);
		
		boolean isChecked = false;
		
		if(!isBlank(checkBox)) isChecked = true;
		
		if(putBack) request.setAttribute(checkBoxName, isChecked);
		
		return isChecked;
	}
	
	/**
	 * Tests for blank parameter.
	 * 
	 * @param parameter
	 * @return boolean
	 */
	private static boolean isBlank(String parameter)
	{
		return (parameter == null || parameter.trim().length() == 0);
	}
	
	/**
	 * Returns a Date from request.
	 * 
	 * @param request
	 * @return Date
	 */
	public static Date getDate(String parameterName, boolean putBack, HttpServletRequest request)
	{
		String parameter = request.getParameter(parameterName);
		
		if(isBlank(parameter)) return null;
		
		try
		{
			Date date = simpleDateFormat.parse(parameter);
			
			if(putBack) request.setAttribute(parameterName, parameter);
			
			return date;
		}
		catch(ParseException e)
		{
			log.error("Error parsing parameter " + parameterName + ".");
			
			return null;
		}
	}
}
